﻿@model S3Upload.Models.UploadModel

@{
    ViewBag.Title = "Upload file to Amazon S3";
}

@section head
{
    <link rel="stylesheet" href="@Url.Content("~/Scripts/uploadify/uploadify.css")" />
    <script type='text/javascript' src="@Url.Content("~/Scripts/uploadify/jquery.uploadify.js")"></script>
}

<script type="text/javascript">
    var fileName = "@Guid.NewGuid().ToString()";
    $(document).ready(function () {
        ResetButtons();
        

        $("#file_upload").uploadify({
            "fileObjName": "file",
            'swf': '/Scripts/uploadify/uploadify.swf',
            'scriptAccess': 'always',
            'buttonText': 'Select file',
            'uploader': "http://s3.amazonaws.com/" + "@Model.Bucket",
            'method': 'post',
            'auto': false,
            'multi': false,
            'queueSizeLimit': 1,
            'successTimeout': 120,
            'formData': {
                "AWSAccessKeyId": "@Model.AccessKey",
                "acl": "public-read",
                "policy": "@Model.Policy",
                "signature": "@Model.Signature",
                "success_action_status": "201",
                "key": "upload/" + fileName
            },
            'onSelect': function (file) {
                fileName = fileName + file.name.substring(file.name.length - 4);
                $("#file_upload").uploadify('settings','formData', {
                    "key": "upload/" + fileName
                });
            },
            'onUploadError': function (file, errorCode, errorMsg, errorString) {
                alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                ResetButtons();
            },
            'onUploadSuccess': function (file, data, response) {
                alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
                ResetButtons();
            },
            'onUploadProgress': function (file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
                $('#progress').html(totalBytesUploaded + ' bytes uploaded of ' + totalBytesTotal + ' bytes.');
            }
        });
    });

    function SetUploadControl(){}
    function  StartUpload() {
        $("#file_upload").uploadify('upload', '*');
        $("#uploadButton").attr('disabled', 'disabled');
        $("#cancelButton").removeAttr('disabled');
    }

    function CancelUpload() {
        $("#file_upload").uploadify('cancel', '*');
        ResetButtons();
    }
    
    function ResetButtons() {
        $("#uploadButton").removeAttr('disabled');
        $("#cancelButton").attr('disabled', 'disabled');
    }
</script>
<h2>Upload file to Amazon S3</h2>
<div align='center'>
   
    <input type='file' id='file_upload' name='file_upload' />
    <div id="progress"></div>
    <div>
        <input id="uploadButton" type="button" value="Upload" onclick="StartUpload();"/>
        <input id="cancelButton" type="button" value="Cancel" onclick="CancelUpload();" disabled="disabled"/>
    </div>
    
</div>